package com.example.user.event;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * 无序事件监听器
 *
 * @author vanson
 * @Async 异步执行
 */
@Slf4j
@Component
public class UserNotifyEventListener {

    @Async("userNotifyPool")
    @EventListener(classes = {UserNotifyEvent.class})
    public void sendTips(UserNotifyEvent event) throws Throwable {
        log.info("[线程-{}]发送用户通知消息:{}", Thread.currentThread().getName(), event.getUser().toString());
        Thread.sleep(1000L);
    }

    @Async("userNotifyPool")
    @EventListener(classes = {UserNotifyEvent.class})
    public void reward(UserNotifyEvent event) throws Throwable {
        log.info("[线程-{}]发送用户奖励消息:{}", Thread.currentThread().getName(), event.getUser().toString());
        Thread.sleep(1000L);
    }

    @Async("userNotifyPool")
    @EventListener(classes = {UserNotifyEvent.class})
    public void deal(UserNotifyEvent event) throws Throwable {
        log.info("[线程-{}]发送用户成交消息:{}", Thread.currentThread().getName(), event.getUser().toString());
        // 验证事件监听器异常的场景
        // log.info("异常场景:{}", 1/0);
    }
}
